﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Template : System.Web.UI.MasterPage
{
    AccountManager am = new AccountManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            initCookies();
        }
    }
    //Xử lí đăng nhập
    public void processLogin(string username, string password)
    {
        int power = am.checkLogin(username, password);
        if (power == 2)
        {
            lblMessage.Text = "Your login attempt was not successful.";
        }
        else
        {
            if (power == 0) //quyen admin
            {
                Session["Username"] = username;
                Session["Power"] = power;
                Session["Fullname"] = AccountManager.fullname;
                if (chkRemember.Checked)
                {
                    writeCookies(username, password);
                }
                Response.Redirect("~/Admin/Default.aspx");
            }
            else //quyen user
            {
                Session["Username"] = username;
                Session["Power"] = power;
                Session["Fullname"] = AccountManager.fullname;
                Session.Add("login", "ok");
                pnelLogin.Visible = false;
                pnelWelcome.Visible = true;
                lblWelcome.Text = "<b>" + Session["Fullname"].ToString() + "</b>";
                if (chkRemember.Checked)
                {
                    writeCookies(username, password);
                }
            }
        }
    }

    //Ghi cookies
    private void writeCookies(string employee_number, string password)
    {
        if (Request.Browser.Cookies)
        {
            if (Request.Cookies["userInfo"] == null)
            {
                HttpCookie ckUserInfo = new HttpCookie("userInfo");
                ckUserInfo["TenDangNhap"] = employee_number;
                ckUserInfo["MatKhau"] = password;
                ckUserInfo.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(ckUserInfo);
            }
        }
    }

    //khoi tao cac cookies neu co lay ve gia tri
    public void initCookies()
    {
        //nếu trình duyệt hỗ trợ cookies
        if (Request.Browser.Cookies)
        {
            if (Request.Cookies.Get("userInfo") != null)
            {
                //Lay cookies nếu có ghi trước đó
                HttpCookie hck = Request.Cookies["userInfo"];
                string user = "";
                string pass = "";
                user = hck["TenDangNhap"];
                pass = hck["MatKhau"];
                //Response.Write("Tên Đăng nhập" + user);//test
                if (user != "" && pass != "")
                {
                    processLogin(user, pass);
                }
            }
        }

        string log = (string)Session["login"];
        // Response.Write("login " + log);
        if (log == null || log == "")
        {

            pnelLogin.Visible = true;
            pnelWelcome.Visible = false;            
        }
        else
        {
            pnelWelcome.Visible = true;
            pnelLogin.Visible = false;
        }

    }
    protected void imgLogin_Click(object sender, ImageClickEventArgs e)
    {
        string uname = txtUsername.Text;
        string pword = txtPassword.Text;
        if (uname == "" || uname == "Username")
        {
            lblMessage.Text = "Please enter username.";
            txtUsername.Focus();
        }
        else if (pword == "" | pword == "Password")
        {
            lblMessage.Text = "Please enter password.";
            txtPassword.Focus();
        }
        else
        {
            processLogin(uname, pword);
        }

    }
}
